<?php

namespace app\company\controller\consult\subset;

use app\admin\service\OnlineExportService;
use app\BaseController;
use app\model\ConsultOnlineModel;
use app\model\UnitModel;
use app\model\UserModel;
use think\App;

class Online extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 线上咨询记录
     * @return bool|string
     */
    public function Index(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['question_id'])) {
                $where[] = ['question_id', 'in', $param['question_id']];
            }
            if (!empty($param['consult_id'])) {
                $where[] = ['consult_id', 'in', $param['consult_id']];
            }
            if (isset($param['is_follow']) && strlen($param['is_follow']) > 0) {
                $where[] = ['is_follow', '=', $param['is_follow']];
            }
            if (isset($param['is_blacklist']) && strlen($param['is_blacklist']) > 0) {
                $where[] = ['is_blacklist', '=', $param['is_blacklist']];
            }
            if (isset($param['is_end']) && strlen($param['is_end']) > 0) {
                $where[] = ['is_end', '=', $param['is_end']];
            }
            if (!empty($param['uid'])) {
                $where[] = ['uid', '=', $param['uid']];
            }
            if (!empty($param['name'])) {
                $UserModel = new UserModel();
                $uids = $UserModel->where('name|username', 'like', '%' . $param['name'] . '%')->column('id');
                if (count($uids)) {
                    $where[] = ['uid', 'in', $uids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['sex'])) {
                //普通用户的性别筛选
                $UserModel = new UserModel();
                $ids = $UserModel->where('sex', '=', $param['sex'])->column('id');
                if (count($ids)) {
                    $where[] = ['uid', 'in', $ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if(!empty($param['class_id'])){
                $UserModel = new UserModel();
                $ids = $UserModel->where('department_id', 'in', $param['class_id'])->column('id');
                if (count($ids)) {
                    $where[] = ['uid', 'in', $ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['id'])){
                $where[] = ['id', 'in', $param['id']];
            }
            $where[] = ['unit_id','=',$param['unit_id']];
            $ConsultOnlineModel = new ConsultOnlineModel();
            $query = $ConsultOnlineModel->where($where);
            $data = $query->with([
                'user', 'consult', 'questionName'
            ])->order('id desc')->paginate($param['pageSize'])->toArray();
            $data['ids'] = $query->column('id');
            $UnitModel = new UnitModel();
            $data['unit_name'] = $UnitModel->where('id','=',$param['unit_id'])->value('name');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     *  线上高级筛选导出
     * @return bool|string
     */
    public function onlineAdvancedFilteringExport(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $param['role_mode'] = 0;
            $param['user_id'] = 0;
            $OnlineExportService = new OnlineExportService($this->app);
            $ret = $OnlineExportService->OnlineExport($param);
            if(!$ret) throw new \Exception($OnlineExportService->getMessage());
            return success($OnlineExportService->getResult());
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}